<!doctype html>
<html>
  <head>
    <title>Notifications: NotificationEvent exposure in a Service Worker.</title>
    <script src="../resources/testharness.js"></script>
    <script src="../resources/testharnessreport.js"></script>
    <script src="../serviceworker/resources/test-helpers.js"></script>
    <script src="resources/test-helpers.js"></script>
  </head>
  <body>
    <script>
      // Tests that the NotificationEvent object is exposed in a ServiceWorker
      // and exposes the "notification", "action" and "waitUntil" members.
      //
      // Because the NotificationEvent must be created with a notification, a
      // notification is shown and clicked on prior to running the actual test
      // in the Service Worker (serviceworker-notification-event.js).
      async_test(function(test) {
          var scope = 'resources/scope/' + location.pathname,
              script = 'resources/serviceworker-notification-event.js';

          testRunner.setPermission('notifications', 'granted', location.origin, location.origin);

          var workerInfo = null;
          getActiveServiceWorkerWithMessagePort(test, script, scope).then(function(info) {
              workerInfo = info;

              // (1) Display a Web Notification from the document.
              assert_inherits(workerInfo.registration, 'showNotification', 'showNotification() must be exposed.');
              return workerInfo.registration.showNotification(scope, {
                  body: 'Hello, world!',
                  icon: '/icon.png'
              });
          }).then(function() {
              // (2) Simulate a click on the notification that has been displayed.
              testRunner.simulateWebNotificationClick(scope);

              workerInfo.port.addEventListener('message', function(event) {
                  if (typeof event.data != 'object' || typeof event.data.success != 'boolean') {
                    assert_unreached('Received an invalid message from the Service Worker.');
                    return;
                  }

                  // (3) Verify that the tests were successful in the Service Worker.
                  assert_true(event.data.success,
                      'Expected tests to pass, but assertion failed:' + '\n' +
                      event.data.message + '\n');

                  test.done();
              });
          }).catch(unreached_rejection(test));

      }, 'The NotificationEvent exposes the expected semantics in a Service Worker.');
    </script>
  </body>
</html>
